import { computed, reactive, ref } from "vue"
import { toast } from "~/composables/util.js";

//列表，搜索，查询，刷新，分页  +  状态修改，删除  按钮
export function useInitTable(opt = {}){
  /* const searchForm = reactive({
    keyword:""
  })
  
  //重置
  const resetSearchForm = ()=>{
    searchForm.keyword = ""
    getData()
  } 
   */
  let searchForm = null
  let resetSearchForm = null
  if(opt.searchForm){
    searchForm = reactive({ ...opt.searchForm })
    resetSearchForm = ()=>{
      for(const key in opt.searchForm){
        searchForm[key] = opt.searchForm[key]
      }
      getData()
    }
  }


  //获取角色列表
  
  
  const tableData =ref([])
  const loading = ref(false)
  //分页部分
  const currentPage = ref(1)  //当前页   页码
  const total = ref(0)        //总条数
  const limit = ref(10)       //每页显示数量
  const list = ref([])
  
  
  /* function getData(){
    tableData.value = [
      {
        "id": 13,
        "title": "nip",
        "content": "nip\n",
        "order": 0,
        "create_time": "2022-06-06 14:40:11",
        "update_time": "2022-06-06 14:40:11"
      }
    ]
  } */
  //获取数据
  function getData(P =null){
    //console.log(p);
    if(typeof P == "number"){ 
      currentPage.value = P
    }
  
    loading.value = true
    opt.getList(currentPage.value,searchForm)
    .then(res=>{
      //console.log(res);
      if(opt.onGetListSuccess && typeof opt.onGetListSuccess == "function"){
        opt.onGetListSuccess(res)
      }else {
        
        tableData.value =res.list
        total.value = res.totalCount
      }
      
    }).finally(()=>{
      loading.value=false
    })
  }
  getData()

  //删除
const handleDelete =(id)=>{
  loading.value = true
  opt.delete(id)
  .then(res=>{
    toast("删除成功")
    getData()
  }).finally(()=>{
    loading.value=false
  })
}


//修改管理员状态
const handleStatusChange =(status,row)=>{
  //console.log(status);
  row.statusLoading = true
  opt.updateStatus(row.id,status)
  .then(res=>{
    toast("修改状态成功")
    row.status = status
  }).finally(()=>{
    row.statusLoading = false
  })
}

//多选
const multiSelectionIds = ref([])
const handleSelectionChange =(e)=>{
  multiSelectionIds.value = e.map(o=>o.id)
  console.log(multiSelectionIds.value);//通过map拿到所有id
} 

//批量删除
const multipleTableRef = ref(null)
const handleMultuiDelete = ()=>{
  loading.value = true
  opt.delete(multiSelectionIds.value)
  .then(res=>{
    toast('删除成功')
    //清空选中
    if(multipleTableRef.value){
      multipleTableRef.value.clearSelection()
    }
    getData()

  }).finally(()=>{
    loading.value = false
  })
}

  //批量上/下架
  const handleMultiStatusChange = (status)=>{
    loading.value = true
    opt.updateStatus(multiSelectionIds.value,status)
    .then(res=>{
      toast('修改状态成功')
      //清空选中
      if(multipleTableRef.value){
        multipleTableRef.value.clearSelection()
      }
      getData()

    }).finally(()=>{
      loading.value = false
    })
  }
  

  return {
    searchForm,
    resetSearchForm,
    tableData,
    loading,
    currentPage,
    total,
    limit,
    getData,
    handleDelete,
    handleStatusChange,
    handleSelectionChange,
    multipleTableRef,
    handleMultuiDelete,
    handleMultiStatusChange
  }
}

//新增，修改 按钮
export function useInitForm(opt = {}){
  //新增
  //表单部分
  const formDrawwerRef = ref(null)
  const formRef = ref(null)
  const defaultForm = opt.form
  const form = reactive({})

  const rules = opt.rules || {}

  //增加和修改共用一个提交方法
  const editId = ref(0)
  const drawerTitle = computed(()=>editId.value ? "修改" : "新增")

  const handleSubmit =()=>{
    formRef.value.validate((valid)=>{
      if(!valid) return

      formDrawwerRef.value.showLoading()

      let body ={}
      if(opt.beforeSubmit && typeof opt.beforeSubmit == "function"){
        body = opt.beforeSubmit({ ...form })
      }else {
        body = form
      }

      //判断是新增还是修改
      const fun = editId.value ? opt.update(editId.value,body) : opt.create(body)

      fun.then(res=>{
        toast(drawerTitle.value +"成功")

        //如果是修改刷新当前页，新增刷新第一页
        opt.getData(editId.value ? false : 1)

        formDrawwerRef.value.close()
      }).finally(()=>{
        formDrawwerRef.value.hideLoading()
      })
    })
  }

  //重置表单
  function resefForm(row=false){
    if(formRef.value) formRef.value.clearValidate()
    for(const key in defaultForm){
      form[key] = row[key]
    }
  }

  //打开抽屉表单,新增
  const handleCreate =()=>{
    editId.value = 0
    resefForm(defaultForm)
    formDrawwerRef.value.open()
  }

  //修改
  const handleUpdate = (row)=>{
    editId.value = row.id
    resefForm(row)
    formDrawwerRef.value.open()
  }

  return {
    formDrawwerRef,
    formRef,
    form,
    rules,
    editId,
    drawerTitle,
    handleSubmit,
    resefForm,
    handleCreate,
    handleUpdate

  }
}

//
export function use(opt = {}){

}